package com.monitor.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.monitor.commons.HttpTookit;
import com.monitor.commons.YXDate;
import com.monitor.config.yunke.YunKeConfig;
import com.monitor.config.yunke.YunKeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.monitor.modelExt.WechatAccountExt;
import com.monitor.dao.IWechatAccountDao;
import com.monitor.searchExt.WechatAccountSearchExt;
import com.monitor.pubInter.IBaseDao;
import com.monitor.pubInter.AbstractBaseService;
import com.monitor.service.IWechatAccountService;
import lombok.extern.slf4j.Slf4j;
import com.monitor.pubInter.exception.SkException;

import java.util.*;


/**
*    @author sunke
*    @Date 2025-04-01 10:48:38
*    @description WechatAccountService      微信账号
*/
@Service(value = "wechatAccountService")
@Slf4j
public class WechatAccountServiceImpl extends AbstractBaseService<WechatAccountExt, WechatAccountSearchExt,String> implements IWechatAccountService {

    @Resource(name = "wechatAccountDao")
    private IWechatAccountDao wechatAccountDao;
    @Autowired
    private YunKeUtil yunKeUtil;
    @Autowired
    private YunKeConfig yunKeConfig;

    @Override
    public IBaseDao<WechatAccountExt, WechatAccountSearchExt,String> getDao() {
        return wechatAccountDao;
    }

    @Override
    public void syncWechatAccountData(Date syncDay) throws SkException {
        String startTime = YXDate.getFormatDate2String(YXDate.getTimeDayFirstSecond(syncDay));
        String endTime = YXDate.getFormatDate2String(YXDate.getTimeDayLastSecond(syncDay));
        wechatAccountData(1, startTime, endTime);
    }

    private void wechatAccountData(int page, String startTime, String endTime) {
        try {
            //定义请求路径
            String url = yunKeConfig.getUrl() + "open/wechat/companyAccounts";
            //组装请求头
            long timestamp = new Date().getTime();
            Map<String, String> headsMap = yunKeUtil.getHeadMap(timestamp);
            //组装请求参数
            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("pageIndex", page);
            paramsMap.put("pageSize", 100);
            paramsMap.put("updateTimeStart", startTime);
            paramsMap.put("updateTimeEnd", endTime);
            String res = HttpTookit.doPostHeader(url, paramsMap, headsMap);
            log.info("获取的微信账号:" + res);
            JSONObject resObject = JSONObject.parseObject(res);
            if (resObject.getBoolean("success")) {

                JSONObject dataObj = resObject.getJSONObject("data");
                int currentPage = dataObj.getInteger("pageIndex");
                int totalPage = dataObj.getInteger("pageCount");
                JSONArray dataArr = dataObj.getJSONArray("page");
                List<WechatAccountExt> wechatAccountExts = new ArrayList<>();
                dataArr.forEach((data) -> {
                    JSONObject item = JSONObject.from(data);
                    JSONArray wechatArr=item.getJSONArray("data");
                    wechatArr.forEach(wechat->{
                        JSONObject wechatObj = JSONObject.from(wechat);
                        WechatAccountExt wechatAccountExt = JSONObject.parseObject(JSONObject.toJSONString(wechat), WechatAccountExt.class);
                        wechatAccountExt.setId(null);
                        wechatAccountExt.setYkUserId(wechatObj.getString("account"));
                        wechatAccountExts.add(wechatAccountExt);
                    });
                });
                wechatAccountDao.batchInsert(wechatAccountExts);

                log.info("第" + page + "页的数据:" + dataArr.size() + "条");
                if (totalPage > currentPage) {
                    currentPage++;

                    wechatAccountData(currentPage, startTime, endTime);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}